var 
    router = require('express').Router(),
    uuid = require('uuid'),
    uploader = require('../utils/fileuploader'),
    extend = require('util')._extend,
    mongos = require('../service/mongodb');

router.delete('/mgt/topic/:id', function (req, res){
    var id = req.params.id;
    var topic;
    var query = {};

    mongos.delete('topics',{'tid':id})
    .then(function(rows){
        res.send({code:200,rows:rows});
    });          



});


router.get('/mgt/tag/:tag', function(req, res) {
    var tag = req.params.tag;

    mongos.find('topics',{tags:tag},'startdate:desc')
    .then(function(docs){
        res.send(docs);  
    });   
});

//list
router.get('/mgt/topics', function(req, res) {
    var all = req.query.all;

    mongos.find('topics',{},'startdate:desc')
    .then(function(docs){
        var _curr = new Date();
        for(var i=0,l=docs.length;i<l;i++){
            docs[i].valid = (docs[i].startdate > _curr);
        }
        res.send(docs);  
    });   
});


router.get('/mgt/topic/:id', function(req, res) {
    var id = req.params.id;
    var topic;
    var query = {};
    if('hot' == id){
        mongos.get('homepageconfig',{})
        .then(function(doc){
            return mongos.get('topics',{'tid':doc.hottopic});
        })
        .then(function(doc){
            doc.valid = (doc.startdate > new Date());
            res.send(doc);             
        });          
    }else{
        mongos.get('topics',{'tid':id})
        .then(function(doc){
            doc.valid = (doc.startdate > new Date());
            res.send(doc);  
        });          
    }   
});

//add topic
router.post('/mgt/topic', function(req, res) {
    var _d = req.body;
    var topic = {
        'title': _d.title,
        'tid': uuid.v1(),
        'created': new Date(),
        'user': _d.user,
        'startdate': new Date(_d.startdate),
        'tags':_d.tags,
        'mkdoc':_d.mkdoc,
        'content': _d.content,
        'status': 1,
        'rate':1,
        'ratenum':1
    };

    if(!!_d.isHomePage){
        mongos.insert('topics',topic)
        .then(function(docs){
            return mongos.update(
                'homepageconfig',{},{hottopic:docs[0].tid});
        })
        .then(function(doc){
            res.send({code:200});            
        });         
    }else{
        mongos.insert('topics',topic)
        .then(function(doc){
            res.send({code:200});
        });         
    }
});

//add topic
router.post('/mgt/fileupload', function(req, res) {
    uploader.upload(req, res, ['md'])
    .then(
    function (path){
        res.send({name:path});
    },
    function (error){
        res.send({error:error});
    }
    );
});


//list
router.post('/mgt/toHomePage', function(req, res) {
    var _d = req.body;

    mongos.update('homepageconfig',{},{hottopic:_d.tid})
    .then(function(d){
        res.send({code:200});  
    });
});

router.put('/mgt/topic/:id', function(req, res) {
    var id = req.params.id;
    var _d = req.body;


    mongos.get('topics', {tid:id})
    .then(function(doc){
        var _doc = extend(doc,_d);
        return mongos.update('topics',{tid:id},_doc);
    })
    .then(function (d){
        res.send({code:200});
    });
});

router.get('/mgt/topic/:tid/enroll', function(req, res) {
    var tid = req.params.tid;
    mongos.find('enroll', {tid:tid})
    .then(function(docs){
        res.send(docs);
    });
});





module.exports = router;
